package servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import pagination.PageBean;
import pojo.Brand;
import service.BrandService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "SelectStatusAllServlet", value = "/selectStatusAllServlet")
public class SelectStatusAllServlet extends HttpServlet {
    BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受当前页码
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.valueOf(_currentPage);
        int pageSize = Integer.valueOf(_pageSize);

//        Integer currentPage = StrUtil.isNotEmpty(_currentPage) ? Integer.parseInt(_currentPage) : null;
//        Integer pageSize = StrUtil.isNotEmpty(_pageSize) ? Integer.parseInt(_pageSize) : null;

        //获取当前条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();

        //转换成Brand
        Brand brand = JSON.parseObject(params,Brand.class);

        //1. 调用BrandService完成查询
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);
        String jsonString = JSON.toJSONString(pageBean);
        //2. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
        request.getRequestDispatcher("/brand.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}